/**
 * Created by admin on 2016/12/13.
 */
'use strict'

let mor = require("../libs/mongo/mongoHelper");
let querystring = require('querystring');
let dbr = require("../libs/mysql/mysqlHelper");
let jv = require("../libs/sys/myHelper").jv;
let http = require('request-promise');

app.use(app.routes.get('/html/order/detail', async ctx => {
  ctx.render("order/detail");
}));

app.use(app.routes.get('/html/order/list', async ctx => {
  ctx.render("order/list");
}));

app.use(app.routes.post('/order/list', async ctx => {
  let params = querystring.parse(ctx.req._parsedUrl.query);

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }


  let skip = ctx.req.json.skip;
  let take = ctx.req.json.take;

  let results = await dbr.find(`select o.id, f.name fuwuName , o.status , o.amount_payable total ,o.contact_name contactor,o.mobile , o.detailed_address address
from thwy_order o
join thwy_fuwu f on ( o.fuwu_id = f.id)
where o.member_id = :userId and o.del_flag = 0 
order by o.create_date desc
limit :skip,:take`,
    {userId: userId, skip: skip, take: take});

  if (!results || !results.length) {
    ctx.body = [];
    return;
  }

  results.forEach(it => {
    it.items = [];
  });

  let items = await dbr.find(`select id, name,quantity,price_payable fee,order_id orderId
from thwy_order_item
where order_id in (${results.map(it => {
    return '"' + it.id + '"';
  }).join(",")}) and del_flag =0 `);
  if (items && items.length) {
    items.forEach(it => {
      results.filter(order => {
        return order.id == it.orderId
      })[0].items.push(it);
    });
  }

  ctx.body = results;
}));


app.use(app.routes.post('/order/detail', async ctx => {

  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let orderId = ctx.req.json.orderId;

  if( !orderId){
    ctx.msg = "找不到订单Id";
    return ;
  }

  let results = await dbr.findOne(`select o.id, f.name fuwuName,f.code fuwu_code,o.status , o.amount_payable total ,o.contact_name contactor,o.mobile , o.detailed_address address ,o.video_link
from thwy_order o
join thwy_fuwu f on (o.fuwu_id = f.id)
where o.id = :orderId and o.member_id = :userId`,
    {orderId: orderId,userId:userId });

  if (!results) {
    return;
  }

  let items = await dbr.find(`select id, name,quantity,price_payable fee,order_id orderId
from thwy_order_item
where order_id  = :orderId `, {orderId: orderId});

  results .items = items;

  ctx.body = results;
}));


app.use(app.routes.post('/order/cancel', async ctx => {
  let userId = ctx.needLogin();
  if (!userId) {
    return;
  }

  let orderId = ctx.req.json.orderId;
  if (!orderId) {
    ctx.msg = "找不到订单Id";
    return;
  }

  let res = await jv.post({
    url: app.appConfig.fontHost + "api/order/updateStatus?id" + orderId + "&status=cancelled&token=" + ctx.session.user_token,
    body: {},
    headers: {"token": ctx.session.user_token}
  });
  if (res.statusCode != 200) {
    jv.log("调用api/order/updateStatus:" + res.statusCode);
    ctx.msg = "调用失败";
    return;
  }

  ctx.body = res.body;
}));
